<?php
include_once("a_checkLogin.php");
include_once("a_getLoginInfo.php");
include_once("conn.php");

if (!isset($_GET['userID'])) {
    die("<script>alert('参数传递出错！未指定信息！');history.go(-1);</script>");
} else {
    $uid = $_GET['userID'];
}
$select_user = "SELECT * FROM user_infos WHERE user_ID = '" . $uid . "'";
if ($result_user = mysqli_query($connect_DB, $select_user)) {
    if (mysqli_num_rows($result_user) < 0) {
        die("<script>alert('未查询到信息！');history.go(-1);</script>");
    } else {
        $row_user = mysqli_fetch_object($result_user);
    }

    $select_order = "SELECT * FROM order_infos WHERE user_ID = '" . $uid . "'";
    $result_order = mysqli_query($connect_DB, $select_order);
    if ($result_order) {
        $num_order = mysqli_num_rows($result_order);
    } else {
        die("<script>alert('查询预约信息出错！');history.go(-1);</script>");
    }

    //设置中国时区
    date_default_timezone_set('Asia/Shanghai');
    // 获取当前日期
    $nowDate = date('Y-m-d');
    $nowTime = date('H:i:s');
    $select_die_order = "SELECT * FROM order_infos WHERE user_ID = '" . $uid . "' AND orderDate < '" . $nowDate . "' AND orderTime < '" . $nowTime . "' AND orderState = '1'";
    $result_die_order = mysqli_query($connect_DB, $select_die_order);
    if ($result_die_order) {
        $num_die_order = mysqli_num_rows($result_die_order);
    } else {
        die("<script>alert('查询超时预约信息出错！');history.go(-1);</script>");
        // echo $select_die_order;
        // exit;
    }

    $select_msg = "SELECT * FROM msg_infos WHERE user_ID = '" . $uid . "'";
    $result_msg = mysqli_query($connect_DB, $select_msg);
    if ($result_msg) {
        $num_msg = mysqli_num_rows($result_msg);
    } else {
        die("<script>alert('查询留言信息出错！');history.go(-1);</script>");
    }

    $select_his_order = "SELECT * FROM order_history_infos WHERE user_ID = '" . $uid . "' AND orderState = '1'";
    $result_his_order = mysqli_query($connect_DB, $select_his_order);
    if ($result_his_order) {
        $num_his_order = mysqli_num_rows($result_his_order);
    } else {
        die("<script>alert('查询历史预约信息出错！');history.go(-1);</script>");
    }

    $select_pet = "SELECT * FROM pet_infos WHERE user_ID = '" . $uid . "'";
    $select_his_pet = "SELECT * FROM history_pet_infos WHERE user_ID = '" . $uid . "'";

    $result_pet = mysqli_query($connect_DB, $select_pet);
    $result_his_pet = mysqli_query($connect_DB, $select_his_pet);
    if (!$result_pet || !$result_his_pet) {
        die("<script>alert('查询宠物信息出错！');history.go(-1);</script>");
    } else {
        $num_pet = mysqli_num_rows($result_pet);
        $num_his_pet = mysqli_num_rows($result_his_pet);
        $num_all_pet = $num_pet + $num_his_pet;
    }

}
?>


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="../public/pic/f.png" type="image/x-icon">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>管理员·用户详细信息</title>
    <link rel="stylesheet" href="../public/css/bootstrap.min.css">
    <script src="../public/js/bootstrap.bundle.min.js"></script>
    <link rel="stylesheet" href="../public/css/adm_public.css">
    <link rel="stylesheet" href="css/adminControlUserInfo.css">
</head>

<body>
    <header class="fixed-top">
        <nav class="navbar navbar-expand-lg nav-top fixed-top">
            <div class="container-fluid">
                <div class="logo-div">
                    <img src="../public/pic/logo-h-w.png">
                </div>
                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                        <li class="nav-item">
                            <a class="nav-link" href="admin_PersonBasicInfo.php">个人基本信息</a>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown"
                                aria-expanded="false">
                                医生管理
                            </a>
                            <ul class="dropdown-menu">
                                <li><a class="dropdown-item" href="admin_AddDoctor.php">新增医生信息</a></li>
                                <li>
                                    <hr class="dropdown-divider">
                                </li>
                                <li><a class="dropdown-item" href="javascript:void(0);">医生信息检索</a></li>
                                <li>
                                    <hr class="dropdown-divider">
                                </li>
                                <li><a class="dropdown-item" href="admin_CreateResetKey.php">重置密码密钥</a></li>
                                <li>
                                    <hr class="dropdown-divider">
                                </li>
                                <li><a class="dropdown-item" href="admin_DepartmentControl.php">医生科室管理</a></li>
                            </ul>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle active" aria-current="page" role="button"
                                data-bs-toggle="dropdown" aria-expanded="false">
                                用户管理
                            </a>
                            <ul class="dropdown-menu">
                                <li><a class="dropdown-item" href="admin_SearchUserInfo.php">用户信息检索</a></li>
                                <li>
                                    <hr class="dropdown-divider">
                                </li>
                                <li><a class="dropdown-item" href="admin_DealDelAccount.php">用户注销审核</a></li>
                            </ul>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown"
                                aria-expanded="false">
                                预约管理
                            </a>
                            <ul class="dropdown-menu">
                                <li><a class="dropdown-item" href="admin_ControlDepartments.php">预约科室信息管理</a></li>
                                <li>
                                    <hr class="dropdown-divider">
                                </li>
                                <li><a class="dropdown-item" href="admin_ControlDieOrders.php">超时信息处理</a></li>
                            </ul>
                        </li>
                        <li class="nav-item dropdown">
                            <a class="nav-link dropdown-toggle" role="button" data-bs-toggle="dropdown"
                                aria-expanded="false">
                                用户主页管理
                            </a>
                            <ul class="dropdown-menu">
                                <li><a class="dropdown-item" href="admin_ControlTips.php">健康知识模块管理</a></li>
                            </ul>
                        </li>
                    </ul>
                </div>
                <div class="info-div">
                    <p>
                        <b>当前登录： </b>
                        <name>
                            <?php echo $admin_Name; ?>
                        </name>| 
                        <a href="admin_Logout.php">退出</a>
                    </p>
                </div>
            </div>
        </nav>
    </header>
    <main>
        <!-- 导航栏 -->
        <div class="main-top-nav">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="li-first">当前位置>>></li>
                    <li class="breadcrumb-item"><a href="javascript:void(0);">用户管理</a></li>
                    <li class="breadcrumb-item"><a href="admin_SearchUserInfo.php">用户信息检索</a></li>
                    <li class="breadcrumb-item active"><a href="javascript:void(0);">用户信息详情</a></li>
                </ol>
                <div class="now-time-div" id="nowtime"></div>
                <script src="../public/js/getnowtime.js"></script>
            </nav>
        </div>

        <div class="main-content-div">
            <div class='all-info-div'>
                <div class='user-info-div'>
                    <div class='b-infos-left'>
                        <h4>用 户 信 息</h4>
                        <p><b>ID：</b>
                            <?php echo $row_user->user_ID; ?>
                        </p>
                        <p><b>昵称：</b>
                            <?php echo $row_user->user_Name; ?>
                        </p>
                        <p><b>电话：</b>
                            <?php echo $row_user->user_Phone; ?>
                        </p>
                    </div>
                    <div class='b-infos-right'>
                        <h4>平 台 信 息</h4>
                        <div class='b-infos-right-s'>
                            <p><b>累计预约：</b>
                                <?php echo $num_his_order; ?> 次
                            </p>
                            <p><b>当前预约：</b>
                                <?php echo $num_order; ?> 条信息
                            </p>
                            <p><b>超时预约：</b>
                                <?php echo $num_die_order; ?> 条信息
                            </p>
                        </div>
                        <div class='b-infos-right-s'>
                            <p><b>留言发布：</b>
                                <?php echo $num_msg; ?> 条信息
                            </p>
                            <p><b>累计登记：</b>
                                <?php echo $num_all_pet; ?> 条信息
                            </p>
                            <p><b>现有登记：</b>
                                <?php echo $num_pet; ?> 条信息
                            </p>
                        </div>
                    </div>
                </div>
                <div class='pet-info-div'>
                    <?php
                    if ($num_pet == 0 && $num_his_pet == 0) {
                        echo"
                            <div class='pet-infos'>
                            <p class='none-info'> 该用户当前暂无宠物信息 </p>
                            </div>";
                    }

                    if ($num_pet > 0) {
                        //设置中国时区
                        date_default_timezone_set('Asia/Shanghai');
                        // 获取当前日期
                        $getNowDate = date('Y-m-d');
                        while ($row_pet = mysqli_fetch_object($result_pet)) {
                            $select_cases = "SELECT * FROM cases_infos WHERE cases_PetID = '".$row_pet->pet_ID."'";
                            $result_cases = mysqli_query($connect_DB, $select_cases);
                            if($result_cases){
                                $num_cases = mysqli_num_rows($result_cases);
                            }else{
                                die("<script>alert('查询病例信息出错！');history.go(-1);</script>");
                            }
                            if ($row_pet->pet_Birthday == '未填写') {
                                $petage = '未知';
                            } else {
                                $bir = new DateTime($row_pet->pet_Birthday);
                                $now = new DateTime($getNowDate);
                                $interval = $now->diff($bir);
                                $petage = $interval->y . '岁' . $interval->m . '月';
                            }

                            if (substr($row_pet->pet_PhotoAddress, 0, 3) == 'def') {
                                $petPhoto = "../public/pic/pet_photos/" . $row_pet->pet_PhotoAddress;
                            } else {
                                $petPhoto = "../public/pic/pet_photos/" . $row_pet->user_ID . "/" . $row_pet->pet_PhotoAddress;
                            }
                            echo "<div class='pet-infos'>
                                            <div class='pet-photo'>
                                                <img src='" . $petPhoto . "'>
                                            </div>
                                            <div class='pet-info'>
                                                <p><b>宠物昵称：</b>" . $row_pet->pet_Name . "</p>
                                                <p><b>宠物类型：</b>" . $row_pet->pet_Type . "</p>
                                                <p><b>添加时间：</b>" . $row_pet->pet_AddDate . "</p>
                                                <p><b>宠物年龄：</b>" . $petage . "</p>
                                            </div>
                                            <div class='pet-status'>
                                                <p><b>信息状态</b></p>
                                                <p>正常</p>
                                            </div>
                                            <div class='pet-cases'>
                                            <p><a href='admin_ControlPetCasesInfo.php?petid=".$row_pet->pet_ID."'><b>病例信息</b></a></p>
                                                <p>".$num_cases."条信息</p>
                                            </div>
                                        </div>";
                        }

                    }

                    if ($num_his_pet > 0) {
                        while ($row_his_pet = mysqli_fetch_object($result_his_pet)) {
                            echo "<div class='pet-infos del-info'>
                                            <div class='pet-info'>
                                                <p><b>宠物ID：</b>" . $row_his_pet->pet_ID . "</p>
                                                <p><b>宠物昵称：</b>" . $row_his_pet->pet_Name . "</p>
                                                <p><b>宠物类型：</b>" . $row_his_pet->pet_Type . "</p>
                                                <p><b>删除时间：</b>" . $row_his_pet->pet_DelDate . "</p>
                                            </div>
                                            <div class='pet-status'>
                                                <p><b>当前信息状态</b></p>
                                                <p>已被用户删除</p>
                                            </div>
                                        </div>";
                        }
                    }
                    ?>
                </div>
            </div>
        </div>
    </main>
    <footer>
        <p>&copy; 2025 ANIMAL HOME PET HOSPITAL. All rights reserved.</p>
    </footer>
</body>

</html>